<?php

class MallGoodsService
{
    /*
    *获取排序方式
    *
    */
    public static function getOrderStr($searchsql, $sort_type)
    {
        $orderstr = "";
        //默认排序
        if ($sort_type == MsgConst::MALLGOODS_SORT_TYPE_DEFAULT) {
            $orderstr = "";

        }

        //评分倒序
        if ($sort_type == MsgConst::MALLGOODS_SORT_TYPE_PRICE_DESC) {
            $orderstr = " order by price desc ";

        }

        //评分倒序
        if ($sort_type == MsgConst::MALLGOODS_SORT_TYPE_PRICE_ASE) {
            $orderstr = " order by price asc ";

        }

        $searchsql = $searchsql . " " . $orderstr;


        return $searchsql;


    }

    /*
    * 根据goods_id查找到对应的规格属性列表
    *
    */
    public static function findspecsandattrs($searchsql,$goods_id)
    {
        $searchsql = $searchsql." and goods_id=".$goods_id;

        $obj = MallGoodsSpecs::model()->findAll($searchsql);

        return $obj;

    }


    /*
    * 生成订单号
    *
    */
    public static function makeordernum()
    {
        $string = '666'.date("YmdHis",time()).rand(100,999);
        return $string;
    }


    /*
    * 生成订单
    *
    */
    public static function createorder($user_id,$inputinfo,$consignee,$moble,$address,$Shipping)
    {
       //获取订单商品列表
        $orderinfo = json_decode($inputinfo);

        $num = 0 ;
        $total_price = 0;

        $obj = MallGoods::model()->findbyPK($orderinfo[0]->goods_id);
        $shop_id = $obj->shop_id;
        foreach($orderinfo as $key =>$value){

           //根据goods_id查找商品详细信息
            $goodobj = MallGoods::model()->findbyPK($value->goods_id);

            //验证是否属于同一家店铺
            if($shop_id != $goodobj->shop_id){
                throw new Exception("商品不完全属于同一家店，由于配送问题，请购买同一家店铺的商品！");
            }

            //计算商品总件数
            $num += $value->num;

            //计算商品总价格
            $total_price += $goodobj->price * MsgConst::MALLORDER_PARENT_DISCOUNT;

            //保存商品数组用于生成子订单
            $goodsobj[$key] = $goodobj;

        }

        //生成主订单
        $parentorder = MallGoodsService::setparentorder($user_id,$num,$total_price,$consignee,$moble,$address,$Shipping);


        //生成子订单
        foreach($orderinfo as $key =>$value){

            //添加
            $obj = new MallOrderChild();
            Util::initObjData($obj);


            $obj->order_num = MallGoodsService::makeordernum();
            $obj->parent_id = $parentorder->id;
            $obj->user_id = $user_id;
            $obj->goods_id = $value->goods_id;
            $obj->goods_name = $goodsobj[$key]->name;
            $obj->goods_pic = $goodsobj[$key]->pic_detail;
            $obj->shop_id = $goodsobj[$key]->shop_id;
            $obj->shop_name = $goodsobj[$key]->shop_name;
            $obj->num = $value->num;
            $obj->price = $goodsobj[$key]->price;
            $obj->discount = MsgConst::MALLORDER_CHILD_DISCOUNT;  //折扣
            $obj->totalprice = $goodsobj[$key]->price * $value->num * MsgConst::MALLORDER_CHILD_DISCOUNT;

            Util::initObjData($obj);
            Util::saveObjData($obj);


        }

        return $parentorder->id;


    }


    /*
    * 生成主订单
    *
    */
    public static function setparentorder($user_id,$num,$total_price,$consignee,$moble,$address,$Shipping)
    {
        $obj = New MallOrderParent();

        $obj->user_id = $user_id;
        $obj->order_num = MallGoodsService::makeordernum();
        $obj->consignee = $consignee;
        $obj->num = $num;
        $obj->total_price = $total_price;
        $obj->moble = $moble;
        $obj->address = $address;
        $obj->status = MsgConst::MALLORDER_STATUE_WAIT_PAYMENT;
        $obj->Shipping = $Shipping;
        $obj->discount = MsgConst::MALLORDER_PARENT_DISCOUNT;

        Util::initObjData($obj);
        Util::saveObjData($obj);

        return $obj;



    }


    /*
    *验证支付方式
    *
    */
    public static function checkpayby($payby)
    {

        switch($payby){
            case MsgConst::MALLORDER_PAYBY_UNDEFINE:
                return false;
            case MsgConst::MALLORDER_PAYBY_PLATFORM:
                return true;
            case MsgConst::MALLORDER_PAYBY_QUICK_PAYMENT:
                return true;
            case MsgConst::MALLORDER_PAYBY_ALIPAY:
                return true;
            default:
                return false;
        }


    }

    /*
    * 根据material_id查找到对应的模板列表
    *
    */
    public static function findtemplatebymaterial($searchsql,$material_id)
    {
        $searchsql = $searchsql." and material_id=".$material_id;

        $obj = MallTemplateMaterial::model()->findAll($searchsql);

        return $obj;

    }

    /*
    * 根据template列表查找到对应的规格列表
    *
    */
    public static function findspecsbytemplate($searchsql,$templatelist)
    {
        $obj = array();
        foreach($templatelist as $value){
            $tem = MallTemplateSpecs::model()->findAll($searchsql." and template_id=".$value->template_id);
            foreach($tem as $value2){

                if(in_array($value2,$obj)==false)
                    array_push($obj,$value2);
            }

        }
        return $obj;

    }

    /*
    * 根据specs列表查找到对应的规格列表
    *
    */
    public static function findspecsandattrsbyspecs($goods_id,$specslist)
    {
        $obj = array();
        foreach($specslist as $value){
            $searchsql = Util::initSearchSql();
            $tem = MallSpecs::model()->findbyPK($value->specs_id);

            //检查是否选择过对应属性
            $mall_goods_specs  = MallGoodsService::checkgoodsandspecs($goods_id,$tem->id);

            if(empty($mall_goods_specs)==true){
                $tem ->is_input = '';
            }else{
                if($mall_goods_specs->is_delete == 1){
                    $tem ->is_input = '';
                }else{
                    $tem ->is_input = $mall_goods_specs;
                }
            }

            //根据规格插入属性内容
            $searchsql = $searchsql." and specs_id=".$value->specs_id;
            $tem ->tem_list = MallSpecsAttrs::model()->findAll($searchsql);

            array_push($obj,$tem);

        }
        return $obj;

    }

    /*
    * 验证是否存在与关系表
    *
    */
    public static function checkgoodsandspecs($goods_id,$specs_id)
    {
        $mall_goods_specs  = MallGoodsSpecs::model()->find("goods_id=:goods_id and  specs_id=:specs_id",
            array(":goods_id" => $goods_id,":specs_id" => $specs_id));

        return $mall_goods_specs;
    }



}

?>